const Controller = require('egg').Controller

class RegisterController extends Controller {
    async index() {
        const { ctx } = this

        await ctx.render('register.html',{
            message: ''
        })
    }

    async getRegister() {
        const { ctx } = this

        /* 接收POST传值 */
        let query = ctx.request.body;

        /* 接收数据库传值 */
        const user=await ctx.service.register.find(query.email);

        if(user==1){
            await ctx.render('register.html',{
                message: '账号已存在，请登录！'
            })
        }else if(user==0){

            const setUserInfo=await ctx.service.register.singUp(query)

            if(setUserInfo){
                ctx.redirect('/login')
            }else{
                ctx.body={
                    code: 500,
                    message:'注册失败！'
                }
            }

        }

    }
}

module.exports = RegisterController